import * as TYPES from './actionTypes';
import {LoginUrl} from '../../Component/common/url';
import { OrderAll } from './orderAction';
// login
export function logIn(opt){
	return (dispatch) => {
		let inner_get = fetch(LoginUrl,{
				method: "POST",
				headers: {
					"Content-Type": "application/json"
				},
				body:JSON.stringify(opt)
			})
			.then(response=>response.json())
			.then((data)=>{
				if(data.data){
					OrderAll(data.data.token)
					dispatch({'type': TYPES.SIGN_IN, 'user': data});
					sessionStorage.username = opt.username;
        			sessionStorage.password = opt.password;
				}else{
					alert("用户名或密码错误。");
					//dispatch({'type': TYPES.SIGN_ERRER});
				}
			}).catch((e)=>{
				console.log(e.message);
				//dispatch({'type': TYPES.SIGN_ERRER, error: e});
			});
	}
}



// skip login
// export function skipLogin(){
// 	return {
// 		'type': TYPES.LOGGED_IN,
// 		'user': skipUser,
// 	}
// }


//logout
export function logOut(){
	return(dispatch)=>{
		dispatch({'type': TYPES.SIGN_OUT})
	}
}

//loginwithwechat
export function logWechat(opt){
	return(dispatch)=>{
		dispatch({'type':TYPE.SIGN_WECHAT,'user':opt})
	}
}


